<?php

namespace App\Http\Controllers\Backend\Content;

use App\Http\Controllers\BaseController;
use App\Interfaces\ArticleInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends BaseController
{
    public function __construct(ArticleInterface $article)
    {
        $this->article = $article;
    }

    public function getArticlePage(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');
        $tenantId = $loginUser['userId'];
        $page = $request->input('page');
        $limit = $request->input('limit');
        $params['cateId'] = $request->input('cateId');
        $params['title'] = $request->input('title');
        $params['user'] = $request->input('user');

        $res = $this->article->getPage($tenantId,$params,$page,$limit);

        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'查询失败！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'SUCCESS!']);
    }

    public function addArticle(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');

        $data = $request->input();

        $data['tenantId'] = $loginUser['tenantId'];

        $data['createTime'] = !empty($data['createTime']) ?$request->input('createTime'):dateTime();

        $data['user'] = !empty($data['user'])?$data['user']:$loginUser['nickname'];

        $data['addNum'] = rand(50,300);

        $res = $this->article->addArticle($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'添加失败！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'添加成功!']);
    }

    public function updateArticle(Request $request): JsonResponse
    {
        $data = $request->input();
        $loginUser = $request->get('userInfo');
        $data['updateTime'] = dateTime();
        $data['user'] = !empty($data['user'])?$data['user']:$loginUser['nickname'];
        $res = $this->article->updateArticle($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'修改失败！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'修改成功!']);
    }

    public function deleteArticle($id): JsonResponse
    {
        $data['id'] = $id;
        $data['deleted'] = 1;
        $data['updateTime'] = dateTime();
        $res = $this->article->updateArticle($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'删除失败！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'删除成功!']);
    }

    public function changeArticleStatus(Request $request): JsonResponse
    {
        $id = $request->input('id');
        $status = $request->input('status');
        $data['id'] = $id;
        $data['updateTime'] = dateTime();
        $data['status'] = $status;
        $res = $this->article->updateArticle($data);
        if (!$res){
            return $this->apiResult(['code'=>1,'message'=>'修改失败！']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'修改成功!']);
    }
}
